package com.yaDuo.common.util;

import android.text.TextUtils;
import android.util.Log;


public class LogUtil {

    private static boolean logEnable = true;

    public static void enableLog(boolean enable) {
        logEnable = enable;
    }

    public static void v(String tag, String content) {
        showLogWithLineNum(tag, Log.VERBOSE, content);
    }

    @Deprecated
    public static void v(String content) {
        showLogWithLineNum(null, Log.VERBOSE, content);
    }


    public static void d(String tag, String content) {
        showLogWithLineNum(tag, Log.DEBUG, content);
    }

    /**
     * 请使用 {@link LogUtils#d(String, String)} 方法
     */
    @Deprecated
    public static void d(String content) {
        showLogWithLineNum(null, Log.DEBUG, content);
    }

    public static void i(String tag, String content) {
        showLogWithLineNum(tag, Log.INFO, content);
    }

    @Deprecated
    public static void i(String content) {
        showLogWithLineNum(null, Log.INFO, content);
    }

    public static void w(String tag, String content) {
        showLogWithLineNum(tag, Log.WARN, content);
    }

    @Deprecated
    public static void w(String content) {
        showLogWithLineNum(null, Log.WARN, content);
    }

    public static void e(String tag, String content) {
        showLogWithLineNum(tag, Log.ERROR, content);
    }

    @Deprecated
    public static void e(String content) {
        showLogWithLineNum(null, Log.ERROR, content);
    }

    private static void showLogWithLineNum(String tag, int logLevel, String content) {
        if (!logEnable) {
            return;
        }
        String[] contents = getAutoJumpLogInfos();
        if (TextUtils.isEmpty(tag) || tag.equals(contents[1])) {
            tag = contents[0] + " " + contents[2];
        } else {
            tag = contents[0] + " [" + tag + "] " + contents[2];
        }
        if (TextUtils.isEmpty(content)) {
            println(logLevel, tag);
            return;
        }

        int index = 0;
        int size = 1024 * 3;

        while (index < content.length()) {
            if (content.length() < (index + size)) {
                size = content.length() - index;
            }
            String subContent = content.substring(index, index + size);
            index += size;
            println(logLevel, tag + ": " + subContent);
        }
    }

    private static void println(int logLevel, String msg) {
        Log.println(logLevel, "TicketUnion", msg);
    }

    /**
     * 获取打印信息所在方法名，行号等信息
     *
     * @return
     */
    private static String[] getAutoJumpLogInfos() {
        String[] contents = new String[]{"", "", ""};
        StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
        if (stackTrace.length < 6) {
//            Log.e(TAG, "Stack is too shallow!!!");
            return contents;
        } else {
            StackTraceElement trace = stackTrace[5];
            // 文件名和行号
            contents[0] = "(" + trace.getFileName() + ":" + trace.getLineNumber() + ")";
            // 类名
            contents[1] = trace.getClassName().substring(trace.getClassName().lastIndexOf(".") + 1);
            // 方法
            contents[2] = trace.getMethodName() + "()";
            return contents;
        }
    }
}
